package site.luojie.controller;

import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import site.luojie.DruidDataSourceFactory;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jie.luo
 * @since 2020-08-13
 */
@RestController
public class ZkConfigController {

    @GetMapping("/example")
    public Map<String, Object> example() throws SQLException {
        Map<String, Object> data = new HashMap<>();

        DruidPooledConnection connection = DruidDataSourceFactory.dataSource.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from example");
        while (resultSet.next()) {
            data.put("id", resultSet.getInt("id"));
            data.put("name", resultSet.getString("name"));
        }
        resultSet.close();
        statement.close();
        connection.close();

        return data;
    }

}
